package com.example.red_tea_house.moudle.Write.model;

import android.util.Log;

import com.example.red_tea_house.annotation.UrlType;
import com.example.red_tea_house.entity.LoginBean;
import com.example.red_tea_house.entity.StoryBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;

import org.litepal.LitePal;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Response;

public class operateStory {
    public static void uploadStory(String title,String content,String time,String custom_tag,int tag,int state,String author)  {
        StoryBean story=new StoryBean();
        story.setTitle(title);story.setContent(content);story.setTime(time);story.setCustom_tag(custom_tag);story.setTag(tag);story.setState(state);story.setAuthor(author);
        story.save();
        /*
        *TODO:发送POST请求时程序会直接闪退，不知道是什么原因,换成GET就好了...
         */
        uploadRemote(story);
    }

    private static void uploadRemote(StoryBean story)  {
        Log.v("test",new Gson().toJson(story));
        OkHttpUtils
                .get()
                .url(UrlType.REMOTESERVER_URL+UrlType.UPLOAD_STORY)
                .addParams("title",story.getTitle())
                .addParams("content",story.getContent())
                .addParams("time",story.getTime())
                .addParams("custom_tag",story.getCustom_tag())
                .addParams("tag",Integer.toString(story.getTag()))
                .addParams("state",Integer.toString(story.getState()))
                /*
                * TODO:tag,state的默认值要设一下
                * */
                .addParams("author",story.getAuthor())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.v("test",e.toString());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.v("test","success");
                    }
                });

    }
    public static List<StoryBean> loadLocalStory(){
        List<StoryBean> stories= LitePal.findAll(StoryBean.class);
        for(int i=stories.size()-1;i>=0;i--){
            StoryBean story= stories.get(i);
            if(story.getContent()==null||story.getTitle()==null){
                stories.remove(story);
            }
        }
        return stories;
    }
    public static List<StoryBean> loadDraft(){
        List<StoryBean> stories= LitePal.findAll(StoryBean.class);
        for(int i=stories.size()-1;i>=0;i--){
            StoryBean story=stories.get(i);
            if(story.getContent()==null||story.getTitle()==null){
                continue;
            }
            stories.remove(story);
        }
        return stories;
    }
}
